﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XWG.JWBST.ProcessInterfaces;
using System.IO;
using System.Web;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using XWG.Framework.AuthUtility;
using XWG.JWBST.JWBSTEntity;
using XWG.JWBST.JWBSTBLL;

namespace XWG.JWBST.DriverRegProcess
{
    /// <summary>
    /// 违章信息驾驶员和车辆信息注册
    /// </summary>
    public class Process : IProcess
    {
        public string ProcessData(StreamReader reader, HttpContext context)
        {
            int num = 3;    //限制车辆数量
            string info = reader.ReadToEnd();
            info = HttpUtility.UrlDecode(info);
            JObject oka = (JObject)JsonConvert.DeserializeObject(info);

            TbBstDriver driver = GetDriverInfo(oka);
            TbBstCar carinfo = GetCarInfo(oka);

            var user = TbBstUserBLL.Load(driver.UserId);

            if (user != null)//判断用户表中是否存在该用户
            {
                if (user.Deltag == 0)//判断用户表中该用户是否被删除
                {
                    if (TbBstDriverBLL.IsExist(driver.UserId,driver.DriverPhone)) //根据UserID和手机号判断这个用户在驾驶员表中是否存在  
                    {
                        if (TbBstCarBLL.CheckNum(carinfo.UserId, num))//判断该用户拥有的车是否超过3辆
                        {
                            if (TbBstCarBLL.CheckRepeat(carinfo.UserId, carinfo.CarCarlicense))//根据用户ID 和车牌号检测重复录入
                            {
                                //记录请求信息
                                JWBSTEntity.TbBstVisits visit = new JWBSTEntity.TbBstVisits();
                                visit.Id = Guid.NewGuid().ToString();
                                visit.Userid = carinfo.UserId;
                                visit.Time = DateTime.Now;
                                visit.Modulename = "BreakRules";
                                visit.Optionname = "驾驶员信息注册和车辆信息注册";
                                visit.Other = oka.ToString();
                                JWBSTBLL.TbBstVisitsBLL.Insert(visit, "", false);

                                bool IsSucessed = false;
                                List<object> items = new List<object>();
                                List<int> op = new List<int>();
                                items.Add(driver);
                                op.Add((int)XWG.Framework.AuthSPConst.OP.Create);
                                items.Add(carinfo);
                                op.Add((int)XWG.Framework.AuthSPConst.OP.Create);
                                XWG.JWBST.JWBSTBLL.Tran.TransOP(ref IsSucessed, items, op);
                                if (IsSucessed)
                                {
                                    return "操作成功！";
                                }
                                else
                                {
                                    return "操作失败!";
                                }

                            }
                            else
                            {
                                return "该车信息已存在";
                            }
                        }
                        else
                        {
                            return "超出车辆数量限定";
                        }
                    }
                    else
                    {
                        if (TbBstCarBLL.CheckNum(carinfo.UserId, num))//判断该用户拥有的车是否超过3辆
                        {
                            if (TbBstCarBLL.CheckRepeat(carinfo.UserId, carinfo.CarCarlicense))//根据用户ID 和车牌号检测重复录入
                            {
                                ResultModel re = new ResultModel();
                                re = TbBstCarBLL.Insert(carinfo, "", false);

                                //记录请求信息
                                JWBSTEntity.TbBstVisits visit = new JWBSTEntity.TbBstVisits();
                                visit.Id = Guid.NewGuid().ToString();
                                visit.Userid = carinfo.UserId;
                                visit.Time = DateTime.Now;
                                visit.Modulename = "BreakRules";
                                visit.Optionname = "车辆信息注册";
                                visit.Other = oka.ToString();
                                JWBSTBLL.TbBstVisitsBLL.Insert(visit, "", false);

                                return re.Message;
                            }
                            else
                            {
                                return "该车信息已存在";
                            }
                        }
                        else
                        {
                            return "超出车辆数量限定";
                        }
                    }
                }
                else
                {
                    return "该用户已被删除";
                }
            }
            else
            {
                return "该用户为未注册手机用户";
            }
        }

        private static TbBstCar GetCarInfo(JObject oka)
        {
            TbBstCar carinfo = new TbBstCar();
            carinfo.CarPkid = Guid.NewGuid().ToString();
            carinfo.UserId = oka["userID"].ToString().Replace("\"", string.Empty);
            carinfo.CarType = oka["carType"].ToString().Replace("\"", string.Empty);   //车辆类型
            carinfo.CarCarlicense = oka["carLicense"].ToString().Replace("\"", string.Empty).ToUpper();   //车牌号
            // carinfo.CarCarframe = DES.Encode(oka["carFrame"].ToString().Replace("\"", string.Empty).ToUpper());   //车架号
            carinfo.CarFramename = oka["carFrameName"].ToString().Replace("\"", string.Empty);  //车牌前信息
            //carinfo.CarPlateColor = oka["plateColor"].ToString().Replace("\"", string.Empty);   //车牌颜色
            carinfo.CarEngineno = oka["engineno"].ToString().Replace("\"", string.Empty);     //发动机号
            carinfo.CarDiscern = oka["discern"].ToString().Replace("\"", string.Empty).ToUpper();   //车辆识别码
            carinfo.Movesign = "0";
            carinfo.Isaudit = "0";
            carinfo.Isdel = "0";
            carinfo.Entrytime = DateTime.Now;
            return carinfo;
        }

        private static TbBstDriver GetDriverInfo(JObject oka)
        {
            TbBstDriver driver = new TbBstDriver();
            driver.DriverPkid = Guid.NewGuid().ToString();
            driver.UserId = oka["userID"].ToString().Replace("\"", string.Empty);
            driver.DriverName = oka["name"].ToString().Replace("\"", string.Empty);
            //driver.DriverCode = DES.Encode(oka["code"].ToString().Replace("\"", string.Empty)); //驾证号
            //driver.DriverAddress = DES.Encode(oka["address"].ToString().Replace("\"", string.Empty));
            driver.DriverScore = "0";  //默认0分
            driver.DriverPhone = DES.Encode(oka["phone"].ToString().Replace("\"", string.Empty));
            driver.DriverIdcard = DES.Encode(oka["IDcard"].ToString().Replace("\"", string.Empty).ToUpper()); //身份证
            driver.Registereddate = DateTime.Now;
            driver.Movesign = "0";  //默认未同步
            driver.Isdel = "0";
            return driver;
        }
    }
}
